package com.xs.ai.controller;


import com.xs.ai.config.BookingTools.BookingDetails;
import com.xs.ai.services.HealerBookingService;
import lombok.SneakyThrows;
import org.springframework.ai.document.Document;
import org.springframework.ai.reader.tika.TikaDocumentReader;
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.redis.RedisVectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@RestController
@CrossOrigin
public class BookingController {

	private final HealerBookingService healerBookingService;

	@Autowired
	private RedisVectorStore vectorStore;

	public BookingController(HealerBookingService healerBookingService) {
		this.healerBookingService = healerBookingService;
	}
	@CrossOrigin
	@GetMapping(value = "/booking/list")
	public List<BookingDetails> getBookings() {
		return healerBookingService.getBookings();
	}
	@SneakyThrows
	@PostMapping("embedding")
	public Boolean embedding(@RequestParam MultipartFile file) {
		// 从IO流中读取文件
		TikaDocumentReader tikaDocumentReader = new TikaDocumentReader(new InputStreamResource(file.getInputStream()));
		// 将文本内容划分成更小的块
		List<Document> splitDocuments = new TokenTextSplitter()
				.apply(tikaDocumentReader.read());
		// 存入向量数据库，这个过程会自动调用embeddingModel,将文本变成向量再存入。
		vectorStore.add(splitDocuments);
		return true;
	}
	@GetMapping("query")
	public List<Document> query(@RequestParam String query) {
		SearchRequest searchRequest = SearchRequest.builder().query(query).topK(5).build();
		//todo  后续可在此实现精确搜索
		return vectorStore.similaritySearch(searchRequest);  //todo 怎么指定选择搜索用的前缀  现在默认时default-index
	}

}
